<?php

/*
 * @copyright :     Ienter asia Technologies
 * @author :        Quyet
 * @version         v.1.0
 * @created         1/7/2014
 * @Description :   Model leave type 
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Mmod_leavetypes extends MYAD_Model {

    protected $table_name = 'm_leavetype';

    function __construct() {
        parent::__construct();
    }

    /*
     * @author :        Quyet
     * @created:        1/7/2014
     * @Description:    Get all data for leave type
     * @Input :         (Integer) $page
     * @Output :        (Array) data for view data
     */

    function getAllData($page = 0) {
        $condition = array(
            'where' => array('CompanyCode' => $this->session->userdata('CompanyCode')),
        );
        /* load all label */
        $labelData = $this->getLabelData();

        /* load all employee data range */
        $allData = $this->countResult($this->table_name, array('CompanyCode' => $this->session->userdata('CompanyCode')));

        /* load pagination */
        $pagiData = array(
            'base_url' => base_url('leave-types'),
            'total_rows' => $allData,
        );
        $pagination = $this->config->item('pagination');
        $offset = isset($pagiData['per_page']) ? $pagiData['per_page'] : $pagination['per_page'];
        $pagi = $this->getPagination($pagiData);

        /* load leavetype data range */
        $condition['limit'] = array($page, $offset);
        $condition['order_by'] = 'LeaveTypeId DESC';
        $leaveType = $this->getLeaveType($condition);

        /* load config */
        $data = array(
            'show' => 'index',
            'pagi' => $pagi,
            'message' => $this->lang->line('lt_table_empty_data'),
            'order' => $page,
        );
        return array_merge($data, $labelData, $leaveType);
    }

    /*
     * @author :        Quyet
     * @created:        1/7/2014
     * @Description:    Get label data of leave type
     * @Input :         void
     * @Output :        (Array) data label
     */

    function getLabelData() {

        $title = $this->lang->line('lt_title');
        $lblNewRegister = $this->lang->line('lt_lblNewRegister');
        $lblOrder = $this->lang->line('lt_table_col_order');
        $lblLeaveTypeName = $this->lang->line('lt_table_col_leavetypename');
        $lblIsPaidLeave = $this->lang->line('lt_table_col_ispaidleave');
        $lblLeaveTypeAction = $this->lang->line('lt_table_col_leavetypeaction');
        $lblLeaveTypeActive = $this->lang->line('po_table_col_Active');

        $data = array(
            'title' => (empty($title) ? 'Leave types' : $title),
            'lblNewRegister' => (empty($lblNewRegister) ? 'New register' : $lblNewRegister),
            'lblOrder' => (empty($lblOrder) ? 'Order' : $lblOrder),
            'lblLeaveTypeName' => (empty($lblLeaveTypeName) ? 'Leave type name' : $lblLeaveTypeName),
            'lblIsPaidLeave' => (empty($lblIsPaidLeave) ? 'is paiad leave' : $lblIsPaidLeave),
            'lblLeaveTypeAction' => (empty($lblLeaveTypeAction) ? 'Action' : $lblLeaveTypeAction),
            'lblLeaveTypeActive' => (empty($lblLeaveTypeActive) ? 'Active' : $lblLeaveTypeActive),
        );
        return $data;
    }

    /*
     * @author :        Quyet
     * @created:        1/7/2014
     * @Description:    Get leave type
     * @Input :         
     *                  (Integer) $employeeID
     *                  (Integer) $limit
     *                  (Integer) $offset
     * @Output :        Array data /FALSE
     */

    function getLeaveType($condition = array()) {
        /* get holiday */
        $temp = $this->selectDB($this->table_name, $condition);
        return array('data' => $temp);
    }

    /*
     * @author :        Quyet
     * @created:        1/7/2014
     * @Description:    check validate form add/edit leave type
     * @Input :         void
     * @Output :        boolean
     */

    function validate() {
        $this->form_validation->set_error_delimiters('', '');
        $this->form_validation->set_rules('txtLeaveTypeName', $this->lang->line('lt_update_lblLeaveTypeName'), 'required|max_length[255]');
        $this->form_validation->set_rules('chkIsPaidLeave', $this->lang->line('lt_update_lblIsPaidLeave'), 'max_length[1]');
        $this->form_validation->set_rules('txtDelete', $this->lang->line('po_update_lblDelete'), 'max_length[1]');
        return $this->form_validation->run();
    }

    /*
     * @author :        Quyet
     * @created:        1/7/2014
     * @Description:    update leave type
     * @Input :         (Integer) $id
     * @Output :        boolean
     */

    function updateLeaveType($leaveTypeIdID = 0) {
        $conditionDuplicate = array(
            'LeaveTypeId !=' => $leaveTypeIdID,
            'CompanyCode' => $this->session->userdata('CompanyCode'),
            'LeaveTypeName' => $this->input->post('txtLeaveTypeName'),
            'Del_Flag' => FALSE,
        );
        $duplicate = $this->countResult($this->table_name, $conditionDuplicate);
        if ($duplicate) {
            return array(
                'flag' => FALSE,
                'message' => $this->lang->line('lt_update_lblLeaveTypeName') . ': ' . $this->lang->line('emp_updateDB_duplicate')
            );
        }
        $data = array(
            'LeaveTypeName' => $this->input->post('txtLeaveTypeName'),
            'IsPaidLeave' => $this->input->post('chkIsPaidLeave'),
            'CompanyCode' => $this->session->userdata('CompanyCode'),
            'Del_Flag' => $this->input->post('txtDelete'),
        );
        $condition = array();
        if ($leaveTypeIdID) {
            $condition['LeaveTypeId'] = (integer) $leaveTypeIdID;
        }
        $temp = $this->updateDB($this->table_name, $data, $condition);
        if ($temp) {
            return array(
                'flag' => TRUE,
                'message' => $this->lang->line('emp_updateDB_success')
            );
        } else {
            return array(
                'flag' => FALSE,
                'message' => $this->lang->line('emp_updateDB_fall')
            );
        }
    }

    /*
     * @author :        Quyet
     * @created:        1/7/2014
     * @Description:    basic data for leave type
     * @Input :         (Interger) $id
     * @Output :        Array
     */

    function getAddLeaveType($leaveTypeIdID = 0) {
        /* get language info */
        $title = ($leaveTypeIdID) ? $this->lang->line('lt_update_title') : $this->lang->line('lt_add_title');
        $dialogTitle = $this->lang->line('emp_dialog_title');
        $lblBack = $this->lang->line('lt_update_lblBack');
        $lblLeaveTypeName = $this->lang->line('lt_update_lblLeaveTypeName');
        $lblIsPaidLeave = $this->lang->line('lt_update_lblIsPaidLeave');
        $lblDelete = $this->lang->line('po_update_lblDelete');
        $lblSubmit = $this->lang->line('lt_update_lblsubmit');

        /* get leave type */
        $condition = array(
            'where' => array('LeaveTypeId' => $leaveTypeIdID),
            'first' => TRUE,
        );
        $leaveTypes = $this->selectDB($this->table_name, $condition);
        if (!isset($_POST['btnSubmit']) && $leaveTypes) {
            /* is load default */
            $emp = array(
                'txtLeaveTypeName' => dQuotes($leaveTypes->LeaveTypeName),
                'chkIsPaidLeave' => $leaveTypes->IsPaidLeave,
                'txtDelete' => $leaveTypes->Del_Flag,
            );
        } else {
            $emp = array(
                'txtLeaveTypeName' => set_value('txtLeaveTypeName'),
                'chkIsPaidLeave' => set_value('chkIsPaidLeave'),
                'txtDelete' => set_value('txtDelete'),
            );
        }

        $temp = array(
            'js' => array('jquery-ui.js'),
            'css' => array('jquery-ui.css'),
            'show' => 'add_leavetypes',
            /* set label */
            'title' => (empty($title) ? 'Leave type registration' : $title),
            'dialogTitle' => (empty($dialogTitle) ? 'Notification' : $dialogTitle),
            'lblBack' => (empty($lblBack) ? 'Back' : $lblBack),
            'lblLeaveTypeName' => (empty($lblLeaveTypeName) ? 'Leave type name' : $lblLeaveTypeName),
            'lblIsPaidLeave' => (empty($lblIsPaidLeave) ? 'Is paid leave' : $lblIsPaidLeave),
            'lblDelete' => (empty($lblDelete) ? 'Delete' : $lblDelete),
            'submit' => (empty($lblSubmit) ? 'Submit' : $lblSubmit),
            /* set data */
            'leaveTypeIdID' => $leaveTypeIdID,
        );
        return array_merge($temp, $emp);
    }

    /*
     * @author :        Quyet
     * @created:        1/7/2014
     * @Description:    basic data for new add leave type
     * @Input :         (Interger) $id
     * @Output :        Array
     * 
     */

    function countLeaveTypeByID($leaveTypeIdID = 0) {
        $condition = array(
            'LeaveTypeId' => $leaveTypeIdID,
            'CompanyCode' => $this->session->userdata('CompanyCode')
        );
        return $this->countResult($this->table_name, $condition);
    }

}

?>
